<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('书架管理列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>书架名称：</label>
                            <input type="text" name="name"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:bookshelf:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
               shiro:hasPermission="system:bookshelf:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="system:bookshelf:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:bookshelf:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">

    var prefix = ctx + "system/bookshelf";
    var selectBookUrl = "http://localhost/system/novel/list";
    var addBookToShelf = "http://localhost/system/details/createBookShelfDetails"
    $(function () {
        var options = {
            url: prefix + "/list",
            readUrl: "http://localhost/system/details/readBook/{id}",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",

            modalName: "书架管理",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '书架编号',
                    visible: false
                },
                {
                    field: 'index',
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1; // 表格的索引从0开始，因此加1
                    }
                },
                {
                    field: 'name',
                    title: '书架名称'
                },
                {
                    field: 'userName',
                    title: '所属用户'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    field: 'updateTime',
                    title: '更新时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="showAddBookDialog(\'' + row.id + '\')"><i class="fa fa-book"></i>添加图书</a>');
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.read(\'' + row.id + '\')"><i class="fa fa-book"></i>查看</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function loadBooks() {
        $.ajax({
            url: selectBookUrl,
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                console.log("data:", data);
                var $bookSelect = $('#bookSelect');
                console.log($bookSelect);
                $bookSelect.empty(); // 清空现有选项
                if (data && data.rows && Array.isArray(data.rows)) {
                    data.rows.forEach(function (book) {
                        console.log(book);
                        var option = $('<option></option>')
                            .attr('value', book.novelId)
                            .text(book.title);
                        $bookSelect.append(option);
                        console.log($bookSelect);
                    });
                }
            },
            error: function (xhr, status, error) {
                console.error('加载图书数据失败:', error);
            }
        });
    }


    function showAddBookDialog(shelfId) {
        // 模拟获取图书列表数据
        loadBooks();

        // 模拟获取图书列表数据
        var html = '<div class="modal fade" id="addBookModal" tabindex="-1" role="dialog" aria-labelledby="addBookModalLabel" aria-hidden="true">' +
            '<div class="modal-dialog" role="document">' +
            '<div class="modal-content">' +
            '<div class="modal-header">' +
            '<h5 class="modal-title" id="addBookModalLabel">为书架添加图书</h5>' +
            '<button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
            '<span aria-hidden="true">&times;</span>' +
            '</button>' +
            '</div>' +
            '<div class="modal-body">' +
            '<select multiple class="form-control" id="bookSelect"></select>' +
            '</div>' +
            '<div class="modal-footer">' +
            '<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>' +
            '<button type="button" class="btn btn-primary" onclick="addBooks(' + shelfId + ')">确定</button>' +
            '</div>' +
            '</div>' +
            '</div>' +
            '</div>';

        $('body').append(html);
        $('#addBookModal').modal('show');
    }

    function addBooks(shelfId) {
        // 获取选中的图书 ID 数组
        var selectedBooks = $('#bookSelect').val();
        console.log("selectedBooks:" + selectedBooks + "shelfId:" + shelfId);
        // 调用后台接口添加图书
        $.ajax({
            url: addBookToShelf,
            type: "POST",
            dataType: 'json',
            data: JSON.stringify({shelfId: shelfId, books: selectedBooks}),
            contentType: "application/json; charset=utf-8",
            success: function (response) {
                $('#addBookModal').modal('hide');
                // 可以根据后台返回结果进行相应处理
                alert('图书添加成功');
            },
            error: function (error) {
                // 处理错误情况
            }
        });
    }
</script>
</body>
</html>